<template>
	<view class="container">
		<!-- 商品信息 -->
		<view class="info-section">
			<view class="section-title">商品信息</view>
			<view class="info-card">
				<view class="info-row">
					<text class="info-label">型号:</text>
					<text class="info-value">{{ productInfo.product_model }}</text>
				</view>
				<view class="info-row">
					<text class="info-label">品牌:</text>
					<text class="info-value">{{ productInfo.brand_name }}</text>
				</view>
				<view class="info-row">
					<text class="info-label">需求数量:</text>
					<text class="info-value">{{ productInfo.required_quantity }}</text>
				</view>
				<view class="info-row">
					<text class="info-label">截止时间:</text>
					<text class="info-value deadline">{{ productInfo.deadline }}</text>
				</view>
				<view class="info-row remark">
					<text class="info-label">备注:</text>
					<text class="info-value">{{ productInfo.note }}</text>
				</view>
			</view>
		</view>

		<!-- 报价信息 -->
		<view class="form-section">
			<view class="section-title">报价信息</view>

			<!-- 报价数量 -->
			<view class="form-item">
				<view class="form-label">报价数量</view>
				<view class="input-with-unit">
					<uv-input type="number" v-model="quoteForm.quantity" placeholder="输入数量"></uv-input>
					<text class="input-unit">个</text>
				</view>
			</view>

			<!-- 报价价格 -->
			<view class="form-item">
				<view class="form-label">报价价格</view>
				<view class="input-with-unit">
					<uv-input type="number" v-model="quoteForm.price" placeholder="输入价格"></uv-input>
					<text class="input-unit">元</text>
				</view>
			</view>

			<!-- 备注 -->
			<view class="form-item">
				<view class="form-label">备注</view>
				<uv-textarea v-model="quoteForm.remark" placeholder="输入备注信息" height="100" maxlength="200"
					count></uv-textarea>
			</view>

			<!-- 上传图片 -->
			<view class="form-item">
				<view class="form-label">上传图片</view>
				<text class="upload-tip">(可上传1张)</text>

				<uv-upload :fileList="fileList1" name="1" multiple :maxCount="1" :previewFullImage="true"
					@afterRead="afterRead" @delete="deletePic"></uv-upload>
				<!-- <view class="upload-area" @click="chooseImage">
					<view class="upload-placeholder" v-if="!quoteForm.image">
						<text class="upload-icon">+</text>
						<text class="upload-text">选择文件</text>
					</view>
					<view class="image-preview" v-else>
						<image :src="quoteForm.image" class="preview-image" mode="aspectFill"></image>
						<view class="remove-image" @click.stop="removeImage">×</view>
					</view>
				</view> -->

				<view class="file-status" v-if="!quoteForm.image">
					未选择任何文件
				</view>
				<view class="file-status" v-else>
					已选择图片
				</view>
			</view>
		</view>

		<!-- 底部操作按钮 -->
		<view class="action-buttons">
			<view class="action-btn cancel" @click="handleCancel">取消</view>
			<view class="action-btn submit" @click="handleSubmit">提交报价</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				productInfo: {},
				fileList1: [],
				quoteForm: {
					quantity: '',
					price: '',
					remark: '',
					image: ''
				}
			};
		},
		onLoad(option) {
			console.log(1111, JSON.parse(option.info))
			if (option.info) {
				this.productInfo = JSON.parse(option.info);
			}
		},
		methods: {
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
				this.quoteForm.image = this.fileList1.map(item => item.url.url).join(',');
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: '/wanlshop.common/upload',
						filePath: url,
						name: 'file',
						success: (res) => {
							let result = JSON.parse(res.data);

							setTimeout(() => {
								resolve(result.data)
							}, 1000)
						}
					});
				})
			},

			// 取消报价
			handleCancel() {
				uni.showModal({
					title: '提示',
					content: '确定要取消报价吗？',
					success: (res) => {
						if (res.confirm) {
							uni.navigateBack();
						}
					}
				});
			},

			// 提交报价
			handleSubmit() {
				// 表单验证
				if (!this.quoteForm.quantity) {
					uni.showToast({
						title: '请输入报价数量',
						icon: 'none'
					});
					return;
				}

				if (!this.quoteForm.price) {
					uni.showToast({
						title: '请输入报价价格',
						icon: 'none'
					});
					return;
				}

				uni.showModal({
					title: '确认提交',
					content: '确定要提交这份报价吗？',
					success: (res) => {
						if (res.confirm) {
							uni.$uv.http.post('/inquiry/quotation', {
								product_id: this.productInfo.id,
								price: this.quoteForm.price,
								quantity: this.quoteForm.quantity,
								image: this.quoteForm.image,
								notes: this.quoteForm.remark,
							}).then(res => {
								if (res.res.code === 1) {
									this.$wanlshop.msg(res.res.msg, 2000, true, 'success');
									setTimeout(() => {
										uni.navigateBack();
									}, 1500);
									return;
								} else {
									this.$wanlshop.msg(res.res.msg, 2000, true, 'none');
								}
								this.loading = false;
							}).catch(error => {
								console.log('error', error)
								this.$wanlshop.msg('获取失败，请稍后重试', 2000, true)
								this.loading = false;
							});
						}
					}
				});
			}
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		padding: 20rpx;
		background-color: #f5f5f5;
		min-height: 100vh;
		box-sizing: border-box;
		padding-bottom: 140rpx;
	}

	.info-section,
	.form-section {
		background-color: #fff;
		border-radius: 12rpx;
		padding: 30rpx;
		margin-bottom: 24rpx;
		box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.05);
	}

	.section-title {
		font-size: 30rpx;
		font-weight: bold;
		color: #333;
		margin-bottom: 24rpx;
		padding-bottom: 20rpx;
		border-bottom: 1rpx solid #f0f0f0;
	}

	.info-card {
		.info-row {
			display: flex;
			margin-bottom: 20rpx;

			&.remark {
				flex-direction: column;

				.info-value {
					margin-top: 10rpx;
					background-color: #f9f9f9;
					padding: 20rpx;
					border-radius: 8rpx;
					line-height: 1.5;
				}
			}

			.info-label {
				font-size: 28rpx;
				color: #666;
				width: 160rpx;
				flex-shrink: 0;
			}

			.info-value {
				font-size: 28rpx;
				color: #333;
				font-weight: 500;
				flex: 1;

				&.deadline {
					color: #fe6600;
					font-weight: bold;
				}
			}
		}
	}

	.form-item {
		margin-bottom: 40rpx;

		.form-label {
			font-size: 28rpx;
			color: #333;
			margin-bottom: 16rpx;
			font-weight: 500;
			display: flex;
			align-items: center;
		}

		.upload-tip {
			font-size: 24rpx;
			color: #999;
			margin-left: 10rpx;
		}

		.input-with-unit {
			position: relative;
			display: flex;
			align-items: center;

			.form-input {
				flex: 1;
				padding: 20rpx 24rpx;
				border: 1rpx solid #e0e0e0;
				border-radius: 8rpx;
				font-size: 28rpx;
				box-sizing: border-box;
				padding-right: 100rpx;
			}

			.input-unit {
				position: absolute;
				right: 24rpx;
				font-size: 28rpx;
				color: #999;
			}
		}

		.form-textarea {
			width: 100%;
			height: 160rpx;
			padding: 20rpx 24rpx;
			border: 1rpx solid #e0e0e0;
			border-radius: 8rpx;
			font-size: 28rpx;
			box-sizing: border-box;
		}

		.upload-area {
			width: 100%;
			height: 200rpx;
			border: 2rpx dashed #e0e0e0;
			border-radius: 8rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			margin-bottom: 16rpx;
			background-color: #fafafa;

			.upload-placeholder {
				display: flex;
				flex-direction: column;
				align-items: center;

				.upload-icon {
					font-size: 48rpx;
					color: #ccc;
					margin-bottom: 10rpx;
				}

				.upload-text {
					font-size: 26rpx;
					color: #999;
				}
			}

			.image-preview {
				position: relative;
				width: 100%;
				height: 100%;

				.preview-image {
					width: 100%;
					height: 100%;
					border-radius: 6rpx;
				}

				.remove-image {
					position: absolute;
					top: 10rpx;
					right: 10rpx;
					width: 40rpx;
					height: 40rpx;
					background-color: rgba(0, 0, 0, 0.5);
					color: #fff;
					border-radius: 50%;
					display: flex;
					align-items: center;
					justify-content: center;
					font-size: 24rpx;
				}
			}
		}

		.file-status {
			font-size: 24rpx;
			color: #999;
			text-align: center;
		}
	}

	.action-buttons {
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
		display: flex;
		padding: 20rpx 30rpx;
		background-color: #fff;
		box-shadow: 0 -4rpx 12rpx rgba(0, 0, 0, 0.05);

		.action-btn {
			flex: 1;
			padding: 24rpx;
			text-align: center;
			border-radius: 8rpx;
			font-size: 30rpx;
			font-weight: 500;
			margin: 0 10rpx;

			&.cancel {
				background-color: #fff;
				color: #666;
				border: 1rpx solid #e0e0e0;
			}

			&.submit {
				background-color: #fe6600;
				color: #fff;
			}
		}
	}

	.placeholder {
		color: #ccc;
		font-size: 28rpx;
	}
</style>